package cn.sxt.sys.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sxt.sys.constast.SYS_Constast;
import cn.sxt.sys.domain.Permission;
import cn.sxt.sys.domain.User;
import cn.sxt.sys.service.LogInfoService;
import cn.sxt.sys.service.PermissionService;
import cn.sxt.sys.utils.ActivierUser;
import cn.sxt.sys.utils.TreeNode;
import cn.sxt.sys.utils.TreeNodeBuilder;
import cn.sxt.sys.vo.LogInfoVo;
import cn.sxt.sys.vo.PermissionVo;
import cn.sxt.sys.vo.UserVo;

@Controller
@RequestMapping("login")
public class LoginController {

	@Autowired
	private PermissionService permissionService;
	
	@Autowired
	private LogInfoService logInfoService;
	
	// 跳到登陆
	@RequestMapping("toLogin")
	public String toLogin() {
		return "system/main/login";
	}

	//登陆
	@RequestMapping("login")
	public String login(UserVo userVo, HttpSession session, Model model,HttpServletRequest request) {
		//shiro主体
		Subject subject = SecurityUtils.getSubject();
		AuthenticationToken token=new UsernamePasswordToken(userVo.getLoginname(), userVo.getPwd());
		try {
			subject.login(token);
			ActivierUser activierUser = (ActivierUser) subject.getPrincipal();
			session.setAttribute("user", activierUser.getCurUser());
			//登陆时添加日志
			LogInfoVo logInfoVo=new LogInfoVo();
			logInfoVo.setLoginname(activierUser.getCurUser().getName()+"-"+activierUser.getCurUser().getLoginname());
			logInfoVo.setLoginip(request.getRemoteAddr());
			logInfoVo.setLogintime(new Date());
			logInfoService.addLogInfo(logInfoVo);
			return "system/main/index";
		} catch (IncorrectCredentialsException e) {
			model.addAttribute("error", "密码不正确");
		} catch (UnknownAccountException e) {
			model.addAttribute("error", "用户名不存在");
		}
		return "system/main/login";
	}
	
	//加载主页左边菜单
	@RequestMapping("loadIndexTree")
	@ResponseBody
	public List<TreeNode> loadIndexTree(HttpSession session){
		User user = (User) session.getAttribute("user");
		List<Permission> list=null;
		//判断超管，查全菜单
		if(user.getType()==SYS_Constast.USER_TYPE_SUPER) {
			PermissionVo permissionVo=new PermissionVo();
			permissionVo.setType(SYS_Constast.PERMISSION_TYPE_MENU);
			permissionVo.setAvailable(SYS_Constast.TYPE_AVAILABLE_YES);
			list=permissionService.queryPermissionForList(permissionVo);
		}else {
			//根据用户id查
			PermissionVo permissionVo=new PermissionVo();
			permissionVo.setType(SYS_Constast.PERMISSION_TYPE_MENU);
			permissionVo.setAvailable(SYS_Constast.TYPE_AVAILABLE_YES);
			list=permissionService.queryPermissionForList(permissionVo,user.getId());
		}
		//构造List<TreeNode>
		List<TreeNode> treeNodes=new ArrayList<>();
		for (Permission p : list) {
			Boolean spread=p.getOpen()==SYS_Constast.TYPE_PUBLIC_ONE?true:false;
			treeNodes.add(new TreeNode(p.getId(), p.getPid(), p.getName(), p.getHref(), 
			 p.getIcon(), spread, p.getTarget()));
		}
		return TreeNodeBuilder.builder(treeNodes, 1);
	}
	
}
